const {
    controller,
    get,
    post,
    put
} = require('../lib/decorator')
const {
    getAllMovies,
    getMovieDetails,
    getRelativeMovies
} = require('../service/movie')

// router 中也可以运行两个中间件，通过 return next() 传递
@controller('api/v0/movies')
export class movieController {
    @get('/')
    async getMovies(ctx, next) {
        const { type, year } = ctx.query
        const movies = await getAllMovies(type, year)
        ctx.body = {
            success: true,
            data: movies
        }
    }

    @get('/:id')
    async getMovieDetail(ctx, next) {
        const { type, year } = ctx.query

        const id = ctx.params.id
        const movie = await getMovieDetails(id)
        const relativeMovies = await getRelativeMovies(movie)
        ctx.body = {
            data: {
                movie,
                relativeMovies
            },
            success: true
        }
    }
}

// router.get('/movies/:id', async (ctx, next) => {
//   const Movie = mongoose.model('Movie')
//   const id = ctx.params.id
//   const movie = await Movie.findOne({ _id:id })
//   ctx.body = {
//     movie
//   }
// })

// module.exports = router